<template>
  <a-modal
    :title="$t('账户修正')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    class="customHeight customHeightChild modal_title"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel" :loading="confirmLoading">{{ $t('取消') }}</a-button>
      <a-button type="primary" @click="handleOk" :loading="confirmLoading">{{ $t('保存') }}</a-button>
    </template>
    <div>
      <a-form-model :model="memberAccount" :rules="rules" ref="memberAccount">
        <a-row>
          <a-col :span="12">
            <a-form-model-item
              :label="$t('手机号')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-bottom: 5px"
              >{{ model.phone }}</a-form-model-item
            >
            <a-form-model-item
              :label="$t('卡号')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-bottom: 5px"
              >{{ cardNos }}</a-form-model-item
            >
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              :label="$t('姓名')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-bottom: 10px"
              >{{ model.name }}</a-form-model-item
            >
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item
              :label="$t('代币')"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-bottom: 10px"
            >
              <a-input disabled v-model="memberAccount.coin" />
            </a-form-model-item>
            <div style="margin-left: 24%; margin-bottom: 10px; color: #e38127">
              {{ $t('代币=真实代币+赠送代币') }}
            </div>
          </a-col>
          <a-col :span="6">
            <a-form-model-item
              :label="$t('真实代币')"
              :labelCol="{ span: 10 }"
              :wrapperCol="{ span: 13 }"
              prop="realCoin"
            >
              <a-input v-model="memberAccount.realCoin" @change="getCoin" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item
              :label="$t('赠送代币')"
              :labelCol="{ span: 10 }"
              :wrapperCol="{ span: 13 }"
              prop="presentCoin"
            >
              <a-input v-model="memberAccount.presentCoin" @change="getCoin" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label="$t('积分')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="integral">
              <a-input v-model="memberAccount.integral" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('彩票')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lottery">
              <a-input v-model="memberAccount.lottery" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('A票')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alottery">
              <a-input v-model="memberAccount.alottery" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('A币')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="points">
              <a-input v-model="memberAccount.points" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('成长值')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="growth">
              <a-input v-model="memberAccount.growth" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('有效期')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-date-picker
                :show-time="true"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请输入有效期')"
                class="query-group-cust"
                v-model="memberAccount.validityEnd"
                style="width: 100%"
              ></a-date-picker>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('会员类型')" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-model="memberAccount.memberTypeId">
                <a-select-option v-for="(item, index) in memberTypeList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('备注')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="points">
              <a-input v-model="comment" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </a-modal>
</template>
<script>
import moment from 'moment'
import { getAction,postAction } from '@/api/manage'

export default {
  name: 'AccountCorrectionModal',
  data() {
    return {
      width: 800,
      visible: false,
      model: {},
      memberAccount: {},
      memberCards: {},
      confirmLoading: false,
      labelCol: {
        span: 6,
      },
      wrapperCol: {
        span: 17,
      },
      cardNos: '',
      rules: {
        realCoin: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        presentCoin: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        integral: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        lottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        alottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        points: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        growth: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
      },
      memberTypeList: [],
      comment:''
    }
  },
  methods: {
    edit(record) {
      getAction('/member/bisMemberRule/list', {notNonmember: 1,pageSize: 50,column: 'createTime',order: 'desc'}).then((res) => {
        if (res.success) {
          this.memberTypeList = res.result.records
        }
      })
      this.comment=''
      this.visible = true
      this.model = Object.assign({}, record)
      this.memberAccount = Object.assign({}, record.memberAccount)
      let coin = (parseFloat(this.memberAccount.presentCoin) + parseFloat(this.memberAccount.realCoin)).toFixed(2)
      this.$set(this.memberAccount, 'coin', coin)
      if (this.model.memberCards.length != 0) {
        this.cardNos = this.model.memberCards
          .map((col) => {
            return col.chipNo
          })
          .join(',')
      }
    },
    getCoin() {
      this.memberAccount.coin = (parseFloat(this.memberAccount.presentCoin) + parseFloat(this.memberAccount.realCoin)).toFixed(2)
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.confirmLoading = true
      let query = {}
      let memberInfo = {}
      let memberAccount = {}
      let order = {}
      order.typeId = 18
      order.comment=this.comment
      memberInfo.id = this.model.id
      memberInfo.memberType = this.memberAccount.memberTypeId
      if (this.model.memberCards.length > 0) {
        memberInfo.chipNo = this.model.memberCards[0].chipNo
      } else {
        memberInfo.chipNo = ''
      }
      memberInfo.tac = this.memberAccount.tac
      memberAccount.alottery = this.memberAccount.alottery
      memberAccount.growth = this.memberAccount.growth
      memberAccount.integral = this.memberAccount.integral
      memberAccount.lottery = this.memberAccount.lottery
      memberAccount.points = this.memberAccount.points
      memberAccount.presentCoin = this.memberAccount.presentCoin
      memberAccount.realCoin = this.memberAccount.realCoin
      memberAccount.validityEnd = this.memberAccount.validityEnd
      memberInfo.memberAccount = memberAccount
      query.memberInfo = memberInfo
      query.order = order
      postAction('/trans/trans', query)
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.visible = false
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
  },
}
</script>